using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using Spacejock;
using System;
using System.Collections;
using System.Reflection;
using System.Text;
using yW5Lib;

namespace yWriter6
{
	public class clsExportToHTML
	{
		private clsPreserveHTM cPreserve;

		public clsExportToHTML()
		{
			cPreserve = new clsPreserveHTM();
		}

		public void ExportToHTML(clsProject cProject)
		{
			clsShared.cSettings.sGet("EditorFontName", "Segoe UI");
			clsShared.cSettings.sGet("EditorFontSize", 10f);
			if (cProject.ChapterCount != 0)
			{
				try
				{
					clsCollection<int> clsCollection = new clsCollection<int>();
					clsRTF clsRTF = new clsRTF(ref clsShared.cLog, ref clsShared.cSettings);
					frmPickChapters frmPickChapters = new frmPickChapters(ref cProject);
					frmPickChapters.ShowDialog();
					if (frmPickChapters.Cancelled)
					{
						return;
					}
					clsCollection = frmPickChapters.PickedIDs;
					string value = "<center>" + cProject.HtmlSceneDivider + "</center>";
					string value2 = "<font name='arial' size=+1>";
					string value3 = "<p align='justify'><i>&nbsp;</i></p>";
					string text = "";
					StringBuilder stringBuilder = new StringBuilder();
					string outputFilename = clsShared.cFileSel.GetOutputFilename("Enter a filename", cProject.ExportFolder(), "Exported Project.html", "*.HTML;*.HTM;*.YBHTM", !clsShared.cSettings.sGet("OverwriteExport", DefaultIfUnknown: false));
					if (Operators.CompareString(outputFilename, "", TextCompare: false) != 0)
					{
						clsShared.cSubs.BusyMouse();
						stringBuilder.AppendLine("<html><head></head><body>");
						stringBuilder.AppendLine(value2);
						IEnumerator enumerator = default(IEnumerator);
						try
						{
							enumerator = clsCollection.GetEnumerator();
							IEnumerator enumerator2 = default(IEnumerator);
							while (enumerator.MoveNext())
							{
								int iD = Conversions.ToInteger(enumerator.Current);
								clsChapter clsChapter = cProject.Chapters.ChapterByID(iD);
								if (outputFilename.ToLower().EndsWith(".ybhtm"))
								{
									stringBuilder.AppendLine("[pb]");
								}
								if (!clsChapter.Title.StartsWith("@#"))
								{
									if (clsChapter.Title.StartsWith("@"))
									{
										stringBuilder.AppendLine("<p class='chapter'>&nbsp;</b></p>");
									}
									else
									{
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										if (clsChapter.Title.Length > 0)
										{
											stringBuilder.AppendLine("<p class='chapter'><b>" + clsShared.cHtml.TextToHtmlConst(clsChapter.Title) + "</b></p>");
										}
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
										stringBuilder.AppendLine(value3);
									}
								}
								int sceneCount = clsChapter.SceneCount;
								for (int i = 1; i <= sceneCount; i++)
								{
									if (frmPickChapters.WithUnused | !cProject.get_SceneByChID(clsChapter.ID, i).Unused)
									{
										clsScene clsScene = cProject.get_SceneByChID(clsChapter.ID, i);
										try
										{
											if ((!clsScene.Unused | frmPickChapters.WithUnused) && (!clsScene.ExportCondSpecific | clsScene.ExportWhenHTML))
											{
												text = modSubs.RTFWithParsedVars(cProject, clsScene, modVariables.eFinalExportType.Html);
												cPreserve.PreserveHtml(ref text);
												string StringToAlter = clsRTF.RtfToHTML(text, IgnoreDocFont: true);
												if (clsRTF.cErrors.Count > 0)
												{
													clsShared.cLog.AddAction("The following errors occurred while exporting scene titled " + clsScene.Title);
													try
													{
														enumerator2 = clsRTF.cErrors.GetEnumerator();
														while (enumerator2.MoveNext())
														{
															string actionString = Conversions.ToString(enumerator2.Current);
															clsShared.cLog.AddAction(actionString);
														}
													}
													finally
													{
														if (enumerator2 is IDisposable)
														{
															(enumerator2 as IDisposable).Dispose();
														}
													}
												}
												clsShared.cHtml.StripBetweenTags(ref StringToAlter, "&lt;TEX ", " /TEX&gt;");
												clsShared.cHtml.StripBetweenTags(ref StringToAlter, "[epub_", "]");
												clsShared.cHtml.StripBetweenTags(ref StringToAlter, "[mobi_", "]");
												clsShared.cText.RemoveCRLFFromEnd(ref StringToAlter);
												try
												{
													StringToAlter = StringToAlter.Trim();
												}
												catch (Exception ex)
												{
													ProjectData.SetProjectError(ex);
													Exception ex2 = ex;
													ProjectData.ClearProjectError();
												}
												cPreserve.RestoreHtml(ref StringToAlter);
												stringBuilder.AppendLine(StringToAlter);
												stringBuilder.AppendLine("");
												if (i < clsChapter.SceneCount)
												{
													clsScene clsScene2 = cProject.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
													if ((!clsScene2.Unused | frmPickChapters.WithUnused) && (!clsScene.ExportCondSpecific | clsScene2.ExportWhenHTML))
													{
														stringBuilder.AppendLine(value);
														stringBuilder.AppendLine("");
													}
												}
											}
										}
										catch (Exception ex3)
										{
											ProjectData.SetProjectError(ex3);
											Exception ex4 = ex3;
											clsShared.cLog.AddAction(ex4, MethodBase.GetCurrentMethod());
											ProjectData.ClearProjectError();
										}
									}
								}
							}
						}
						finally
						{
							if (enumerator is IDisposable)
							{
								(enumerator as IDisposable).Dispose();
							}
						}
						stringBuilder.AppendLine("</font>");
						stringBuilder.AppendLine("</body></html>");
						clsShared.cFile.WriteTextFile(outputFilename, stringBuilder.ToString());
						clsShared.cSubs.NormalMouse();
						clsShared.cFile.RunFile(outputFilename);
					}
				}
				catch (Exception ex5)
				{
					ProjectData.SetProjectError(ex5);
					Exception ex6 = ex5;
					clsShared.cLog.ShowMinorError(ex6, MethodBase.GetCurrentMethod());
					ProjectData.ClearProjectError();
				}
				clsShared.cLog.WriteActionsToLog();
			}
		}
	}
}
